package com.music.content.persistnce;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class FileUploadBean {

   private String savePath, filepath, filename, contentType;   
   private Dictionary formfields;

   public String getFilename() {
      return filename;
   }

   public String getFilepath() {
      return filepath;
   }

   public void setSavePath(String savePath) {
      this.savePath = savePath;
   }

   public String getContentType() {
      return contentType;
   }

   public String getFieldValue(String fieldName) {
      if (formfields == null || fieldName == null)
         return null;
         return (String) formfields.get(fieldName);
      }

    private void setFilename(String s) {
       if (s==null) return;

       int pos = s.indexOf("filename=\"");
       if (pos != -1) {
          filepath = s.substring(pos+10, s.length()-1);          
          pos = filepath.lastIndexOf("\\");
          if (pos != -1) filename = filepath.substring(pos + 1);
             else filename = filepath;
       }
    }

    private void setContentType(String s) {
       if (s==null) return;

       int pos = s.indexOf(": ");
       if (pos != -1)
          contentType = s.substring(pos+2, s.length());
    }   

   public void doUpload(HttpServletRequest request) throws IOException {
     
      ServletInputStream in = request.getInputStream();
      byte[] line = new byte[1280];
      int i = in.readLine(line, 0, 1280);

      if (i < 3) return;

      int limitLength = i - 2;
      String limit = new String(line, 0, limitLength); //-2 discards the newline character
      formfields = new Hashtable();

      while (i != -1) {
         String newLine = new String(line, 0, i);
         if (newLine.startsWith("Content-Disposition: form-data; name=\"")) {
            if (newLine.indexOf("filename=\"") != -1) {
               setFilename(new String(line, 0, i-2));
               if (filename==null) return;

               //this is the file content
               i = in.readLine(line, 0, 1280);
               setContentType(new String(line, 0, i-2));
               i = in.readLine(line, 0, 1280);
 
               // blank line
               i = in.readLine(line, 0, 1280);
               newLine = new String(line, 0, i,"ISO8859_1");
               FileOutputStream pw=new FileOutputStream((savePath==null? "" : savePath) + "//" + filename);

               //PrintWriter pw = new PrintWriter(new BufferedWriter(new
               //FileWriter((savePath==null? "" : savePath) + "//" + filename)));
               while (i != -1 && !newLine.startsWith(limit)) {                  
                  i = in.readLine(line, 0, 1280);
                  if ((i==limitLength+2 || i==limitLength+4) && (new String(line, 0, i).startsWith(limit)))
                     pw.write(newLine.substring(0, newLine.length()-2).getBytes("ISO8859_1"));
                  else
                     pw.write(newLine.getBytes("ISO8859_1"));

                  newLine = new String(line, 0, i,"ISO8859_1");
               }
               pw.close();
           } else {
                  //this is a field
                  // get the field name
                  int pos = newLine.indexOf("name=\"");
                  String fieldName = newLine.substring(pos+6, newLine.length()-3);
                  i = in.readLine(line, 0, 1280);
                  i = in.readLine(line, 0, 1280);
                  newLine = new String(line, 0, i);
                  StringBuffer fieldValue = new StringBuffer(1280);
                  while (i != -1 && !newLine.startsWith(limit)) {                  
                    i = in.readLine(line, 0, 1280);
                    if ((i==limitLength+2 || i==limitLength+4) && (new String(line, 0, i).startsWith(limit)))
                       fieldValue.append(newLine.substring(0, newLine.length()-2));
                    else
                       fieldValue.append(newLine);

                    newLine = new String(line, 0, i);
                  }

                  formfields.put(fieldName, fieldValue.toString());
                  }
       }
       i = in.readLine(line, 0, 1280);
    }
  }

}
